
#include "h264_one_frame_get.h"

#define READ_SIZE		(100 * 1024)
#define CACHE_SIZE      100     //   size = ((CACHE_SIZE + 500) % 1024) * 1024     more than max frame size
#define IN_FILENAME		"dec.h264"

H264FileSource file_s;

int main(void)
{
	h264_one_frame_get_init(&file_s, 100);	
	
	int ret = 0;
    FILE *in_file = NULL,*out_file = NULL;
	
    char *frame_buffer = NULL;
    frame_buffer = (char *)malloc(READ_SIZE);
	
	char * dst_video_buffer = NULL;
	dst_video_buffer = (char *)malloc(10000000);
	
	in_file = fopen(IN_FILENAME,"r");
    if(in_file == NULL)
    {
        printf("open in file error!\n");
    }
    
	int test = 0;
	int times = 40;
    char frame_name[30] = "";
	int frame_num = 0;
	while(1)
	{
		memset(frame_buffer,0,READ_SIZE);
		ret = fread(frame_buffer,READ_SIZE,1,in_file);
		if(ret != 1)
		{
			printf("ret = %d\n");
			printf("fread file error!\n");
		}
		int buffer_l = 0;

	//	printf("file:%s line:%d\n",__FILE__,__LINE__);
		int read_long = READ_SIZE;
		while(h264_one_frame_get_process(&file_s, (char *)frame_buffer, read_long,dst_video_buffer, &buffer_l)!= 2)
		{
			if(test++ == times)
				break;
			if (buffer_l > 0)
			{
			//	printf("xubinbin file:%s line:%d\n",__FILE__,__LINE__);
				printf("frame_num = *%d*       buffer_l = *%d*\n",frame_num++,buffer_l);
				#if 0
				sprintf(frame_name,"%d.264",frame_num);
				out_file = fopen(frame_name,"w");
				if(out_file == NULL)
				{
					printf("open in file error!\n");
				}
					
				ret = fwrite(dst_video_buffer,buffer_l,1,out_file);
				if(ret != 1)
				{
					printf("ret = %d\n");
					printf("fwrite file error!\n");
				}
				fclose(out_file);
				#endif
			}
			buffer_l = 0;
			read_long = 0;
		}

		if(test >= times)
			break;
	}
	free(frame_buffer);
	fclose(in_file);
	h264_one_frame_get_deinit(&file_s);
    printf("Done!\n");
}





  





